package com.hkd.user_api.config;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HKD
 * @date 2022/9/4 17:39
 */
@EnableSwagger2WebMvc
@Configuration
public class SwaggerConfig {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${swagger.default-login.enable}")
    private boolean loginEnable;
    @Value("${swagger.default-login.user.account}")
    private String loginAccount;
    @Value("${swagger.default-login.user.password}")
    private String loginPassword;
    /**
     * 创建API
     */
    @Bean
    public Docket createRestApi() {
        Docket build = new Docket(DocumentationType.SWAGGER_2)
                // 详细定制
                .apiInfo(apiInfo("1.0.0"))
                .select()
                // 指定扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("com.hkd.user_api.controller"))
                // 扫描所有
                .paths(PathSelectors.any())
                .build();
        //如果需要在swagger上带入token
        if (loginEnable) {
            build.globalOperationParameters(defaultHeader());
        }
        return build;
    }

    /**
     * 鉴权
     *
     * @return
     */
    private  List<Parameter> defaultHeader() {
        String String = restTemplate.postForObject("http://127.0.0.1:9001/auth/oauth/token?grant_type=password&username="+loginAccount+"&password="+loginPassword+"&client_id=curl_client&client_secret=admin123","", String.class);
        JSONObject jsonObject = JSONObject.parseObject(String);
        java.lang.String token = jsonObject.getString("access_token");
        ParameterBuilder builder = new ParameterBuilder();
        Parameter parameter1 = builder.name("Authorization")
                .description("oauth2鉴权")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue(token)
                .required(false).build();
        List<Parameter> list=new ArrayList<>();
        list.add(parameter1);
        return list;
    }

    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo(String version) {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                .title("HKD112233")
                .version(version)
                .build();
    }

}
